﻿namespace Furesoft.Addins
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class TypeResolver
    {
        #region Static Fields

        private static List<Type> types;

        #endregion

        #region Public Methods and Operators

        public static Type[] GetTypes()
        {
            return types.ToArray();
        }

        public static IEnumerable<Type> GetTypes(string basetype)
        {
            foreach (var t in types)
            {
                if (t.BaseType.Name == basetype)
                {
                    yield return t;
                }
            }
        }

        public static void Init(Assembly ass)
        {
            if(types == null)
                types = new List<Type>();

            foreach (Type type in ass.GetTypes())
            {
                if (type.IsPublic)
                {
                    types.Add(type);
                }
            }
        }

        public static void Init(Assembly[] ass)
        {
            foreach (var assembly in ass)
            {
                Init(assembly);   
            }
        }

        #endregion
    }
}